<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" href="../css/common.css">
		<link rel="stylesheet" href="../css/main.css">
		<title>后台管理-类别管理</title>
		<script type="text/javascript" src="../js/jquery.min.js"></script>
		<script type="text/javascript" src="../js/showpic.js"></script>
		<script type="text/javascript" src="../js/ajaxfileupload.js"></script>
		<script type="text/javascript">
			$(function() {
				finds();
			})

			function addTypeInfo() {
				var tname = $.trim($("#tname").val());
				if (tname == null) {
					return;
				}

				var fileLen = $("#photo")[0].files.length;
				if (fileLen <= 0) {
					return;
				}

				$.ajaxFileUpload({
					url: "../../type/add",
					secureuri: false,
					fileElementId: "photo",
					dataType: "json",
					data: {
						tname: tname
					},
					success: function(data, status) {
						if (data.code == 200) {
							$("#myform")[0].reset();
							$("#show_pic").html("");
						} else {
							alert("添加商品类型失败...");
						}
					},
					error: function(data, status, e) {
						alert("添加商品类型失败...\n" + e);
					}
				})
			}
			var statusStr = ["已下载", "在架"];

			function finds() {
				$.get("../../type/findAll", null, rt => {
					if (rt.code == 200) {
						var str = "";
						$.each(rt.data, function(index, item) {
							str += "<tr><td><img src='../../../" + item.pic +
								"' width='100px' height='100px'/></td><td>";
							str += item.tno + "</td><td>" + item.tname + "</td><td>" + statusStr[item.status] +
								"</td>";
							if (item.status == 0) {
								str += "<td><a href='javascript:update(" + item.tno +
									",1)' style='color:green'>上架</a></td></tr>";
							} else {
								str += "<td><a href='javascript:update(" + item.tno +
									",0)' style='color:red'>下架</a></td></tr>";
							}
						})
						$("#goodstype_info").html("").append($(str));
					} else {
						$("#goodstype_info").html("");
					}
				}, "json");
			}

			function update(tno, status) {
				$.post("../../type/update", {
					tno: tno,
					status: status
				}, rt => {
					if (rt.code == 200) {
						finds();
					} else {
						alert("类型修改失败,请稍后重试...");
					}
				}, "json");
			}
		</script>
	</head>
	<body>
		<div id="forms" class="mt10">
			<div class="box">
				<div class="box_border">
					<div class="box_center">
						<form id="myform">
							<table id="type">
								<tr>
									<td>类别编号:<input type="text" id="tno" class="input-text lh30" size="40" /></td>
									<td>书籍类别:<input type="text" id="tname" class="input-text lh30" size="40" /></td>
								</tr>
								<tr>
									<td colspan="2"><input type="button" name="button" class="btn btn82 btn_save2" onclick="addTypeInfo()" value="添加" /></td>
								</tr>
				
									
								
							</table>
							
							
							
						</form>
					</div>
					<div id="show_pic"></div>
				</div>
			</div>
		</div>
		<div id="table" class="mt10">
			<div class="box span10 oh">
				<table width="100%" border="0" cellpadding="0" cellspacing="0" class="list_table">
					<thead>
						<tr>
							<th width="25%">类型编号</th>
							<th width="25%">类型名称</th>
							<th width="25%">类型状态</th>
							<th>操作</th>
						</tr>
					</thead>
					<tbody id="goodstype_info" align="center">

					</tbody>
				</table>
				<div class="page mt10">
					<div class="pagination">
						<ul>
							<li class="first-child"><a href="#">首页</a></li>
							<li class="disabled"><span>上一页</span></li>
							<li class="active"><span>1</span></li>
							<li><a href="#">2</a></li>
							<li><a href="#">下一页</a></li>
							<li class="last-child"><a href="#">末页</a></li>
						</ul>
					</div>
				</div>
			</div>
		</div>
	</body>
</html>
